package com.aurora.app.service;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aurora.app.dao.CollectionThingStoryDao;
import com.aurora.app.dao.ThingStoryDao;
import com.aurora.app.entity.CollectionThing;
import com.aurora.app.entity.CollectionThingStory;
import com.aurora.app.entity.Thing;
import com.aurora.app.entity.ThingStory;
import com.aurora.app.entity.User;
import com.aurora.app.enums.FakeDelete;
import com.aurora.app.exption.NullEntityException;
import com.aurora.base.dao.BaseDao;
import com.aurora.base.dto.Order;
import com.aurora.base.dto.QueryResult;
import com.aurora.base.service.BaseService;
@Service
public class CollectionThingStoryService extends
		BaseService<CollectionThingStory> {
	@Resource
	private CollectionThingStoryDao baseDao;
	@Resource
	private ThingStoryDao thingStoryDao;
	
	@Override
	public BaseDao<CollectionThingStory> getBaseDao() {
		// TODO 自动生成的方法存根
		return this.baseDao;
	}
	
	/**
	 * 用户收藏一个物语
	 * @param thingStory
	 * @param user
	 * @throws Exception
	 */
	@Transactional
	public void add(ThingStory thingStory,User user) throws Exception{
		CollectionThingStory cts = new CollectionThingStory();
		cts.setThingStory(thingStory);
		cts.setUser(user);
		this.baseDao.persist(cts);
	}
	
	/**
	 * 获取用户收藏的物语
	 * @param userId
	 * @return
	 */
	@Transactional
	public List<CollectionThingStory> getListByUserWithPages(HttpServletRequest request,String userId){
		Order order = new Order();
		order.put("createTime", Order.DESC);
		
		QueryResult<CollectionThingStory> res = this.baseDao.query(request, order,"o.user.id = ? and o.isDelete = 0", userId);
		request.setAttribute("count", res.getCount());
		return res.getResults();
	}
	
	
	/**
	 * 判断此用户是否收藏该故事
	 * @param user
	 * @param thingStory
	 * @return
	 */
	public Boolean isCollection(User user,ThingStory thingStory){
		List<CollectionThingStory> list = this.baseDao.list("o.thingStory = ? and o.user = ? and o.isDelete = 0", thingStory,user);
		if(list!=null&&!list.isEmpty()){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取此thingStory的关注人的列表
	 * @param thingStory
	 * @param index
	 * @param size
	 * @return
	 */
	public List<CollectionThingStory> getCollectionUser(ThingStory thingStory,int index,int size){
		Order order = new Order();
		order.put("createTime", Order.DESC);
		if(index==-1||size==-1){
			return this.baseDao.list(order,"o.thingStory = ?", thingStory);
		}
		return this.baseDao.list(order, index, size,  "o.thingStory = ?", thingStory);
	}
	
	/**
	 * 获取此thingStory关注数量
	 * @param thingStory
	 * @return
	 */
	public long getCollectionNum(ThingStory thingStory){
		return this.baseDao.getCount("o.thingStory = ?", thingStory);
	}
	
	/**
	 * 用户取消关注一个闲置
	 * @param thing
	 * @param user
	 * @throws Exception
	 */
	@Transactional
	public void remove(ThingStory thingStory,User user) throws Exception{
		CollectionThingStory ct = this.baseDao.find("o.thingStory = ? and o.user = ? and o.isDelete = 0", thingStory,user);
		if(ct!= null){
			ct.setIsDelete(FakeDelete.ISDELETE);
			this.baseDao.merge(ct);
			int zan = thingStory.getZan();
			zan = zan-1;
			if(zan<0){
				zan = 0;
			}
			thingStory.setZan(zan);
			this.thingStoryDao.merge(thingStory);
		}else{
			throw new NullEntityException("没有此物语");
		}
		
	}

}
